#include "blockQueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

void* consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while (true)
    {
        Task t;
        bq->pop(&t);
        t();
        std::cout << pthread_self() << " | consumer data: " << t.formatAvg() << t.formatRes() << std::endl;
    }
}

void* productor(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    std::string opers = "+-*/%";
    while (true)
    {
        sleep(1);
        int x = rand() % 20 + 1;
        int y = rand() % 10 + 1;
        char op = opers[rand() % opers.size()];
        Task t(x, y, op);
        bq->push(t);
        std::cout << pthread_self() << " | productor data: " << t.formatAvg() << "?" << std::endl;
    }
}


int main()
{
    srand((uint64_t)time(nullptr) ^ getpid());
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}